import 'package:flutter/material.dart';
import 'package:shenling_wuliu/component/goodItem.dart';
import 'package:shenling_wuliu/utils/http.dart';
import 'package:shenling_wuliu/utils/toast.dart';

class TaskWidget extends StatefulWidget {
  const TaskWidget({super.key});

  @override
  State<TaskWidget> createState() => __TaskWidgetState();
}

class __TaskWidgetState extends State<TaskWidget> {
  final Color customColor = const Color(0xFFEF4F3F);
  List titleList = [
    {"title": "待提货"},
    {"title": "在途"},
    {"title": "已完成"},
  ];
  List<Tab> tabs = 
  [
    Tab(text: '待提货'),
    Tab(text: '在途'),
    Tab(text: '已完成'),
  ];
  //请求参数
  Map<String, dynamic> waitTaskParams = {
    "page": 1,
    "pageSize": 10,
    "status": 1,
  };
  Map<String, dynamic> tranTaskParams = {
    "page": 1,
    "pageSize": 10,
    "status": 2,
  };
  Map<String, dynamic> taskedParams = {
    "page": 1,
    "pageSize": 10,
    "status": 6,
  };
  Map waitTaskMap = {};
  Map tranTaskMap ={};
  Map taskedMap = {};
  int currentIndex = 0;
  @override
  void initState() {
    super.initState();
    getTaskList();
  }

  getTaskList() async{
    try{
        var waitRes = await http.get('/driver/tasks/list',params: waitTaskParams);
        setState(() {
          waitTaskMap = waitRes['data'];
        });
        
    } catch(e) {
      ToastUtil.showError('请求任务数据失败');
    }
  }
  getTaskByIndex(int index) async{
    if(index == 0) {
      // getTaskList();
      var waitRes = await http.get('/driver/tasks/list',params: waitTaskParams);
      setState(() {
        waitTaskMap = waitRes['data'];
        // print(waitRes);
      });
    } else if(index == 1) {
      //在途
      var tranRes = await http.get('/driver/tasks/list',params: tranTaskParams);
      setState(() {
        tranTaskMap = tranRes['data'];
      });
    } else if(index == 2) {
      var taskedRes = await http.get('/driver/tasks/list',params: taskedParams);
      setState(() {
        taskedMap = taskedRes['data'];
      });
    }
  }
  Widget buildTaskList(List? items) {
  if (items == null || items.isEmpty) {
    String title = '';
    if(currentIndex == 0) {
      title = '无待提货物';
    } else if(currentIndex == 1) {
      title = '无在途任务';
    } else if(currentIndex == 2) {
      title = '无已完成任务';
    }
    return Container(
      height: MediaQuery.of(context).size.height,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Image.asset('ic_not_found.png'),
          SizedBox(height: 20,),
          Text(title),
        ],
      ),
    );
  }

  return ListView.builder(
    itemCount: items.length,
    itemBuilder: (context, index) => Column(
      children: [
        Gooditem(goodItem: items[index]),
        SizedBox(height: 15,)
      ],
    ),
  );
}
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromARGB(255, 247, 241, 241),
      body: DefaultTabController(
              length: tabs.length, // tab数量
              child: Column(
                children: [
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 16),
                    decoration: BoxDecoration(
                      color: Colors.white,
                    ),
                    child: TabBar(
                      isScrollable: false, // 不滚动，均分宽度
                      labelColor: Colors.black87,
                      unselectedLabelColor: Colors.grey,
                      labelStyle: TextStyle(fontWeight: FontWeight.bold),
                      indicatorColor: customColor,
                      indicatorSize: TabBarIndicatorSize.label, // 下划线长度与文字一致
                      tabs: tabs,
                      onTap: (index){
                        // print('触发事件拉');
                        setState(() {
                          currentIndex = index;
                        });
                        getTaskByIndex(index);
                      },
                    ),
                  ),
                  Expanded(
                    child: TabBarView(
                      children: [
                        buildTaskList(waitTaskMap['items']),
                        buildTaskList(tranTaskMap['items']),
                        buildTaskList(taskedMap['items'])
                      ],
                    ),
                  ),
                ],
              ),
            ),
    );
  }
}